/*
 * @Author: yuki
 * @Date: 2025-11-04 11:33:20
 * @LastEditTime: 2025-11-06 18:03:58
 * @Description: eslint 配置文件
 */
// @ts-check
import eslint from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import importPlugin from 'eslint-plugin-import';
import eslintConfigPrettier from 'eslint-config-prettier';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';

export const baseConfig = {
  extends: [
    eslint.configs.recommended,
    ...tseslint.configs.strictTypeChecked,
    ...tseslint.configs.stylisticTypeChecked,
    ...tseslint.configs.recommended,
    eslintConfigPrettier,
    eslintPluginPrettierRecommended
  ],
  ignores: [
    'node_modules/**',
    'dist/**',
    'build/**',
    '*.d.ts',
    'eslint.config.*',
    'prettier.config.*'
  ],
  plugins: {
    import: importPlugin
  },
  languageOptions: {
    globals: {
      ...globals.node,
      ...globals.jest,
      ...globals.browser
    },
    ecmaVersion: 2022,
    sourceType: 'module',
    parserOptions: {
      projectService: true,
      tsconfigRootDir: import.meta.dirname,
      allowDefaultProject: true
    },
    parser: tseslint.parser
  },
  rules: {
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/no-floating-promises': 'warn',
    '@typescript-eslint/no-unsafe-argument': 'off',
    '@typescript-eslint/no-non-null-assertion': 'off',
    '@typescript-eslint/no-unsafe-member-access': 'warn',
    '@typescript-eslint/no-extraneous-class': 'off',
    '@typescript-eslint/no-unsafe-return': 'off',
    '@typescript-eslint/no-unnecessary-type-parameters': 'off',
    '@typescript-eslint/no-misused-promises': 'off',

    // 无用变量和未使用变量的规则
    '@typescript-eslint/no-unused-vars': [
      'error',
      {
        vars: 'all',
        args: 'after-used',
        argsIgnorePattern: '^_',
        caughtErrors: 'all',
        caughtErrorsIgnorePattern: '^_',
        destructuredArrayIgnorePattern: '^_',
        ignoreRestSiblings: true
      }
    ],
    'no-unused-vars': 'off',

    // TypeScript 类型导入规则
    '@typescript-eslint/consistent-type-imports': [
      'error',
      {
        prefer: 'type-imports',
        fixStyle: 'separate-type-imports',
        disallowTypeAnnotations: false
      }
    ],

    // prettier
    'prettier/prettier': 'warn',

    'no-console': 'warn',

    'import/no-unresolved': 'error',
    'import/no-duplicates': [
      'error',
      {
        considerQueryString: true
      }
    ],
    // 2. 强制类型导入使用 import type，与值导入分离
    '@typescript-eslint/consistent-type-imports': [
      'error',
      {
        prefer: 'type-imports', // 优先用 import type 导入类型
        disallowTypeAnnotations: false
      }
    ],
    // import 排序规则
    'import/order': [
      'error',
      {
        groups: [
          'builtin',
          'external',
          'internal',
          ['parent', 'sibling'],
          'index',
          'object'
        ],
        pathGroups: [
          {
            pattern: '@/**',
            group: 'internal',
            position: 'after'
          },
          {
            pattern: '**/node_modules/**',
            group: 'external',
            position: 'before'
          }
        ],
        pathGroupsExcludedImportTypes: ['builtin'],
        'newlines-between': 'always',
        alphabetize: {
          order: 'asc',
          caseInsensitive: true
        }
      }
    ]
  }
};

export default tseslint.config(baseConfig);
